<?php

namespace app\admin\controller;

use think\Db;

class Record extends Common{

	/*
	资金记录
	 */
	public function zj(){
		return $this->fetch();
	}
	
	public function dataList(){
		$limit   = request()->param('limit')?:20;
		$address = request()->param('address');
		$where = ' 1=1';
		if($address != ''){
			$where .= ' and u.address like \'%'.$address.'%\'';
		}
		if ($this->data_uids_string) {
			$where .= ' and b.uid in ('.$this->data_uids_string.')';
		}
		// var_dump($this->data_uids);die();
		$data  = Db::table('tp_balance_record')
				->alias('b')
				->join('tp_user u','u.id = b.uid','LEFT')
				->where($where)
				->field('b.*,u.address')
				->order('b.id desc')
				->paginate($limit);	
		$data = $data->toArray();
		if(empty($data['data'])){
			$data['data']  = Db::table('tp_balance_record')
				->alias('b')
				->join('tp_user u','u.id = b.uid','LEFT')
				->where($where)
				->field('b.*,u.address')
				->order('b.id desc')
				->page(1)
				->limit($limit)
				->select();		
		}
		return $this->tableData($data);
	}

	public function out(){
		return $this->fetch();
	}
	
	public function outList(){
		$limit   = request()->param('limit')?:20;
		$status = request()->param('status');
		$address = request()->param('address');
		$where = '1=1';
		if($address != ''){
			$where .= ' and b.address like \'%'.$address.'%\'';
		}
		if($status != '') {
            $where .= ' and b.status='.$status;
        }
        if ($this->data_uids_string) {
			$where .= ' and b.uid in ('.$this->data_uids_string.')';
		}
        // $data = Db::table('tp_out_record')->select();
        // var_dump($where);die();
		$data  = Db::table('tp_out_record')
				->alias('b')
				->join('tp_user u','u.id = b.uid','LEFT')
				->where($where)
				->field('b.*')
				->order('b.id desc')
				->paginate($limit);	
		$data = $data->toArray();
		if(empty($data['data'])){
			$data['data']  = Db::table('tp_out_record')
				->alias('b')
				->join('tp_user u','u.id = b.uid','LEFT')
				->where($where)
				->field('b.*')
				->order('b.id desc')
				->page(1)
				->limit($limit)
				->select();		
		}
		return $this->tableData($data);
	}

	/*
	  提币审核
	 */
	public function pass() {
		if (request()->method() == 'POST') {
			$data = [];
			$id = intval(request()->param('id'));
			$remark = request()->param('value');
			
			Db::startTrans();
			$info = Db::table('tp_out_record')->where('id', $id)->lock(true)->find();
			if ($info['status'] != 0) {
				Db::rollback();
				return ['code' => 1, 'msg' => '订单状态错误'];
			}
			$res = Db::table('tp_out_record')->where('id', $id)->update([
				'status'=>1,
				'remark' => '已通过',
				'check_remark'=>$remark,
				'change_date'=>date('Y-m-d H:i:s')
			]);
			if (!$res) {
				Db::rollback();
				return ['code' => 1, 'msg' => '操作失败'];
			}
			Db::commit();
			return ['code' => 0, 'msg' => '操作成功'];
		}
	}

	/*
	  提币拒绝
	 */
	public function refuse() {
		if (request()->method() == 'POST') {
			$data = [];
			$id = intval(request()->param('id'));
			$remark = request()->param('value');
			$info = Db::table('tp_out_record')->where('id', $id)->find();
			if ($info['status'] != 0) {
				return ['code' => 1, 'msg' => '订单状态错误'];
			}
			Db::startTrans();
			$res1 = Db::table('tp_out_record')->where('id', $id)->update([
				'status'=>-1,
				'remark' =>'已拒绝',
				'check_remark'=>$remark,
				'change_date'=>date('Y-m-d H:i:s')
			]);
			$res2 = Db::table('tp_user')->where('id',$p_user['id'])->setInc('balance',$info['amount']);
			$res3 = Db::table('tp_balance_record')->insert([
					'uid'=>$info['id'],
					'type'=>23,
		            'name'=>'USDT',
					'balance'=>$info['bnb_amount'],
					'date'=>date('Y-m-d H:i:s'),
					'remark'=>'提币退回',
					'detail_remark'=>'提币退回'
			]);
			if ($res1 && $res && $res3) {
				Db::commit();
				return ['code' => 0, 'msg' => '操作成功'];
			} else {
				Db::rollback();
				return ['code' => 1, 'msg' => '操作失败'];
			}
		}
	}
}